#ifndef SORT_H_INCLUDED
#define SORT_H_INCLUDED

#include<stdlib.h>

void sort(PNODE tree);//排序节点兄弟
void sort_vector(unsigned int *num, vector<PNODE> &data);
void sort_list(PNODE tree) ;
void print_tree(PNODE tree,ifstream &data);
void update_dic(PNODE tree);//更新字典库

void sort_tree(PNODE tree) {
    if (tree->feature.size()!=0) {
        sort(tree->feature.begin(),tree->feature.end(),descending);
        }
    if (tree->value.size()!=0) {
        sort(tree->value.begin(),tree->value.end(),descending);
        }
    if(tree->son!=NULL) {
        sort_list(tree);
        PNODE tmp=tree->son;
        while(tmp!=NULL) {
            sort_tree(tmp);
            tmp=tmp->brother;
            }
        }
    }

void sort_vector(unsigned int *num, vector<PNODE> &data) {
    unsigned int i,j,max,size=data.size();
    vector<PNODE>::iterator it=data.begin();
    unsigned int tmp;
    PNODE stmp;
    for(i=0; i<size-1; i++) {
        for(j=i+1,max=i,tmp=num[max]; j<size; j++) {
            if(num[j]<tmp) {
                tmp=num[j];
                max=j;
                }
            }
        num[max]=num[i];
        num[i]=tmp;
        stmp=it[i];
        it[i]=it[max];
        it[max]=stmp;
        }
    }

void sort_list(PNODE tree) {
    vector<PNODE> list;
    PNODE tmp=tree->son;
    while(tmp!=NULL) {
        list.push_back(tmp);
        tmp=tmp->brother;
        }
    unsigned int *num=new unsigned int[list.size()];
    tmp=tree->son;
    unsigned int i=0;
    while(tmp!=NULL) {
        num[i++]=atoi((tmp->tag).c_str());
        tmp=tmp->brother;
        }
    sort_vector(num, list);
    delete num;

    vector<PNODE>::iterator it=list.begin();
    i=0;
    while(i<list.size()-1) {
        (it[i])->brother=it[i+1];
        ++i;
        }
    (it[i])->brother=NULL;
    tree->son=it[0];
    (it[0])->father=tree;
    }

void print_tree(PNODE tree,ofstream &file,int deep) {
    if (tree==NULL) return;
    for(int i=deep; i!=0; i--) file<<" ";
    file<<tree->num_total<<'@'<<tree->name<<endl;
    tree->num_total=0;//输出后重置0
    if (tree->feature.size()!=0) {
        for(int i=deep; i!=0; i--) file<<" ";
        file<<"f ";

        for (vector<CONTENT>::iterator it = tree->feature.begin(); it != tree->feature.end(); ++it ) {
            file<<(*it).num<<'@'<<(*it).nickname<<"&&";
            (*it).num=0;
            }
        file<<endl;
        }
    if (tree->value.size()!=0) {
        for(int i=deep; i!=0; i--) file<<" ";
        file<<"v ";

        for (vector<CONTENT>::iterator it = tree->value.begin(); it != tree->value.end(); ++it ) {
            file<<(*it).num<<'@'<<(*it).nickname<<"&&";
            (*it).num=0;
            }
        file<<endl;
        }
    PNODE tmp=tree->son;
    while(tmp!=NULL) {
        print_tree(tmp,file,deep+1);
        tmp=tmp->brother;
        }
    }

void update_dic(PNODE tree) {
    if (tree==NULL) return;
    tree->num_total=0;//输出后重置0
    ofstream file;
    if (tree->feature.size()!=0) {
        file.open((tree->path+"/feature.txt").c_str());
        for (vector<CONTENT>::iterator it = tree->feature.begin(); it != tree->feature.end(); ++it ) {
            file<<(*it).nickname<<"->"<<(*it).name<<endl;
            (*it).num=0;
            }
        file.close();
        }
    if (tree->value.size()!=0) {
        file.open((tree->path).c_str());
        for (vector<CONTENT>::iterator it = tree->value.begin(); it != tree->value.end(); ++it ) {
            file<<(*it).nickname<<"->"<<(*it).name<<endl;
            (*it).num=0;
            }
        file.close();
        }
    PNODE tmp=tree->son;
    while(tmp!=NULL) {
        update_dic(tmp);
        tmp=tmp->brother;
        }
    }


#endif // SORT_H_INCLUDED
